#include "gptim.h"

bool gptimer_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data)
{
    // gpio_set_level(GPIO_NUM_4,!gpio_get_level(GPIO_NUM_4));
    return true;
}

bool gptimer2_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data)
{
    // gpio_set_level(GPIO_NUM_5,!gpio_get_level(GPIO_NUM_5));
    return true;
}
bool gptimer3_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data)
{
    // gpio_set_level(GPIO_NUM_6,!gpio_get_level(GPIO_NUM_6));
    return true;
}

bool gptimer4_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data)
{
    // gpio_set_level(GPIO_NUM_7,!gpio_get_level(GPIO_NUM_7));
    return true;
}




void timer_init(uint32_t hz, uint64_t arr)
{
    gptimer_handle_t gptimer_handle = {0};
    gptimer_config_t config = {
        .clk_src = GPTIMER_CLK_SRC_DEFAULT,  //默认APB总线，基本不会改
        .direction = GPTIMER_COUNT_UP,       //向上计数
        .resolution_hz = hz,        //1MHz
    };
    gptimer_new_timer(&config, &gptimer_handle);

    gptimer_alarm_config_t alarm_config = {
        .alarm_count = arr,
        .reload_count = 0,  //重装载回到0
        .flags.auto_reload_on_alarm =  true,

    };
    gptimer_set_alarm_action(gptimer_handle,&alarm_config);  //设置警报动作函数

    const gptimer_event_callbacks_t cbs = {
        .on_alarm = gptimer_callback,
    };
    gptimer_register_event_callbacks(gptimer_handle, &cbs, NULL);
    gptimer_enable(gptimer_handle);
    gptimer_start(gptimer_handle); 

}





void timer2_init(uint32_t hz, uint64_t arr)
{
    gptimer_handle_t gptimer_handle = {0};
    gptimer_config_t config = {
        .clk_src = GPTIMER_CLK_SRC_DEFAULT,  //默认APB总线，基本不会改
        .direction = GPTIMER_COUNT_UP,       //向上计数
        .resolution_hz = hz,        //1MHz
    };
    gptimer_new_timer(&config, &gptimer_handle);

    gptimer_alarm_config_t alarm_config = {
        .alarm_count = arr,
        .reload_count = 0,  //重装载回到0
        .flags.auto_reload_on_alarm =  true,

    };
    gptimer_set_alarm_action(gptimer_handle,&alarm_config);  //设置警报动作函数

    const gptimer_event_callbacks_t cbs = {
        .on_alarm = gptimer2_callback,
    };
    gptimer_register_event_callbacks(gptimer_handle, &cbs, NULL);
    gptimer_enable(gptimer_handle);
    gptimer_start(gptimer_handle); 

}


void timer3_init(uint32_t hz, uint64_t arr)
{
    gptimer_handle_t gptimer_handle = {0};
    gptimer_config_t config = {
        .clk_src = GPTIMER_CLK_SRC_DEFAULT,  //默认APB总线，基本不会改
        .direction = GPTIMER_COUNT_UP,       //向上计数
        .resolution_hz = hz,        //1MHz
    };
    gptimer_new_timer(&config, &gptimer_handle);

    gptimer_alarm_config_t alarm_config = {
        .alarm_count = arr,
        .reload_count = 0,  //重装载回到0
        .flags.auto_reload_on_alarm =  true,

    };
    gptimer_set_alarm_action(gptimer_handle,&alarm_config);  //设置警报动作函数

    const gptimer_event_callbacks_t cbs = {
        .on_alarm = gptimer3_callback,
    };
    gptimer_register_event_callbacks(gptimer_handle, &cbs, NULL);
    gptimer_enable(gptimer_handle);
    gptimer_start(gptimer_handle); 

}


void timer4_init(uint32_t hz, uint64_t arr)
{
    gptimer_handle_t gptimer_handle = {0};
    gptimer_config_t config = {
        .clk_src = GPTIMER_CLK_SRC_DEFAULT,  //默认APB总线，基本不会改
        .direction = GPTIMER_COUNT_UP,       //向上计数
        .resolution_hz = hz,        //1MHz
    };
    gptimer_new_timer(&config, &gptimer_handle);

    gptimer_alarm_config_t alarm_config = {
        .alarm_count = arr,
        .reload_count = 0,  //重装载回到0
        .flags.auto_reload_on_alarm =  true,

    };
    gptimer_set_alarm_action(gptimer_handle,&alarm_config);  //设置警报动作函数

    const gptimer_event_callbacks_t cbs = {
        .on_alarm = gptimer4_callback,
    };
    gptimer_register_event_callbacks(gptimer_handle, &cbs, NULL);
    gptimer_enable(gptimer_handle);
    gptimer_start(gptimer_handle); 

}